﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using static RabbitDownload.WebSite.Local.TagManger.LocalTagStatcivalue;

namespace RabbitDownload.WebSite.Konachan.Tag
{
    internal class KonachanTagScanner
    {
        /// <summary>
        /// 从文件扫描相关类
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Score"></param>
        /// <param name="Rating"></param>
        /// <param name="Start"></param>
        /// <param name="End"></param>
        /// <param name="ExcludeFlag"></param>
        /// <param name="OpenLog"></param>
        public void StartWithFile(string FilePath, String Score, string Rating, string Start, string End, bool ExcludeFlag)
        {
            List<string> TagList = new SiteCurrencyHelper().GetDownoadProcess(FilePath, "//item");

            while (TagList.Count > 0)
            {
                string TagInfo = TagList[0];
                TagList.RemoveAt(0);
                new SiteCurrencyHelper().SaveDownloadProcess(FilePath, TagList);
                TranslateStruct translateStruct = new TranslateStruct(TagInfo);
                Console.WriteLine("扫描标签{0}，罗马音标签{1}", translateStruct.ZhTag, translateStruct.RomaTag);
                if (translateStruct.ZhTag == "" || translateStruct.RomaTag == "")
                    continue;
                new KonachanTagScanner().Start(translateStruct.RomaTag.Trim(), Score, Rating, Start, End, ExcludeFlag, false, translateStruct.ZhTag);
            }
        }

        /// <summary>
        /// 启动对标签的扫描
        /// </summary>
        /// <param name="Tag">标签</param>
        /// <param name="Score">评分</param>
        /// <param name="Rating">评级</param>
        /// <param name="Start">起始页</param>
        /// <param name="End">结束页</param>
        /// <param name="ExcludeFlag">是否启用屏蔽</param>
        public void Start(string Tag, String Score, string Rating, string Start, string End, bool ExcludeFlag, bool OpenLog, string FileNameTip = "")
        {
            String API = string.Format(KonachanStaticValue.TagApi, "{0}",  WebHelper.UrlEncode(Tag.Trim().Replace(" ", "_")));
            int ScoreCount = TextHelper.StringToInt(Score);
            int StartPage = TextHelper.StringToInt(Start);
            int EndPage = TextHelper.StringToInt(End);
            List<string> ImgList = GetALLImg(API, ScoreCount, StartPage, EndPage, Rating, ExcludeFlag, OpenLog);
            // if(ImgList.Count>0)
            WebCurrency.SaveHelper.AutoSave(ImgList, "Konachan", FileNameTip + Tag);
        }

        private List<string> GetALLImg(string API, int ScoreCount, int StartPage, int EndPage, string Rating, bool ExcludeFlag, bool OpenLog)
        {
            string Shtml = WebHelper.HttpPcGet(API);
            List<string> AllTempList = new List<string>();

            int JumpCount = 3;
            for (; StartPage <= EndPage || EndPage == 0; StartPage++)
            {
                List<string> Temp = new KonachanTool().GetImgList(string.Format(API, StartPage), Rating, ScoreCount, ExcludeFlag, OpenLog);
                if (Temp.Count == 0)
                {
                    Console.WriteLine("未抓取到内容，距离结束还有{0}次", --JumpCount);
                    if (JumpCount == 0)
                        break;
                }
                else JumpCount = 3;
                AllTempList.AddRange(Temp);
            }
            return AllTempList;
        }
    }
}